package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtRecharge;
import com.greyu.ysj.entity.CbtRewardExample;
import com.greyu.ysj.entity.CbtReward;
import com.greyu.ysj.entity.CbtRewardExample;
import com.greyu.ysj.mapper.CbtRewardMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtRewardService;

@Service
public class CbtRewardServiceImpl implements CbtRewardService {
	@Autowired
	private CbtRewardMapper cbtRewardMapper;

	@Override
	public ResultModel save(CbtReward cbtReward) {
		if(cbtReward.getUserId() == null) {
			return ResultModel.error(ResultStatus.DATA_NOT_NULL);
		}
		if(cbtReward.getUserId() != 0) {
			
			this.cbtRewardMapper.insert(cbtReward);
		}
		return ResultModel.ok(cbtReward);
	}
	@Override
	public CbtReward selectRewardByuserId(int userId) {
		CbtReward signIn=this.cbtRewardMapper.selectRewardByuserId(userId);
		return signIn;
	}
	@Override
	public CbtReward selectRewardByrefereeId(int userId) {
		CbtRewardExample cbtRewardExample=new CbtRewardExample();
		CbtRewardExample.Criteria criteria=cbtRewardExample.createCriteria();
		criteria.andRefereeEqualTo(userId);
		List<CbtReward>list=this.cbtRewardMapper.selectByExample(cbtRewardExample);
		
		CbtReward cbtReward;
		try {
			cbtReward = list.get(0);
        } catch (Exception e) {
            return null;
        }
        return cbtReward;
	}
	@Override
	public double selectTotalRewardByuserId(int userId) {
		double amount=this.cbtRewardMapper.selectTotalRewardByuserId(userId);
		return amount;
	}
	
	@Override
	public List<CbtReward> getRewardRecordByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtRewardExample cbtRewardExample=new CbtRewardExample();
		CbtRewardExample.Criteria criteria=cbtRewardExample.createCriteria();
		criteria.andFlagEqualTo(2);
		criteria.andUserIdEqualTo(userId);
		cbtRewardExample.setOrderByClause("`reward_date` DESC");
		List<CbtReward>list=this.cbtRewardMapper.selectByExample(cbtRewardExample);
		return list;
	}
	@Override
	public int totalPages(Integer userId, Integer rows) {
		CbtRewardExample cbtRewardExample = new CbtRewardExample();
		CbtRewardExample.Criteria criteria = cbtRewardExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
		criteria.andFlagEqualTo(2);
		long total=this.cbtRewardMapper.countByExample(cbtRewardExample);
		int totalPages;
		if(total%rows==0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}
	@Override
	public ResultModel update(int id, CbtReward cbtReward) {
		CbtReward newOne=this.cbtRewardMapper.selectByPrimaryKey(id);
		// 找不到 user
        if (newOne == null) {
            return ResultModel.error(ResultStatus.DATA_NOT_FOUND);
        }
        if(cbtReward.getAmount() != null) {
        	newOne.setAmount(cbtReward.getAmount());
        }
        if(cbtReward.getFlag() != null) {
        	newOne.setFlag(cbtReward.getFlag());
        }
        if(cbtReward.getReferee() != null) {
        	newOne.setReferee(cbtReward.getReferee());
        }
        if(cbtReward.getRegisterNo() != null) {
        	newOne.setRegisterNo(cbtReward.getRegisterNo());
        }
        if(cbtReward.getUserId() != null) {
        	newOne.setUserId(cbtReward.getUserId());
        }
        if(cbtReward.getRewardDate() != null) {
        	newOne.setRewardDate(cbtReward.getRewardDate());
        }
        this.cbtRewardMapper.updateByPrimaryKey(newOne);
		return ResultModel.ok(newOne);
	}
	@Override
	public CbtReward selectByuserId(int userId) {
		CbtRewardExample cbtRewardExample=new CbtRewardExample();
		CbtRewardExample.Criteria criteria=cbtRewardExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<CbtReward>list=this.cbtRewardMapper.selectByExample(cbtRewardExample);
		
		CbtReward cbtReward;
		try {
			cbtReward = list.get(0);
        } catch (Exception e) {
            return null;
        }
        return cbtReward;
	}

}
